<?php
	class W_user_mod extends Model{
		function W_user_mod(){
			parent::Model();
		}
        
        function getData($field, $tabel, $where='',$group_by='') {
            $this->db->select($field);
            if ($where!='') {
                $this->db->where($where);
            }
            if ($group_by!='') {
                $this->db->group_by($group_by);
            }
            $query = $this->db->get($tabel);
            if ($query->num_rows() > 0) {
                if ($row = $query->row_array()) {
                    return $row[$field];
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        
        
        
        function list_user($where="", $num=0, $offset=0, $ro=false) {
            
            $this->db->from('user');
             
            
            if ($where!='') {
                $this->db->where($where);
            }
            
            if ($num > 0) {
                $this->db->limit($num, $offset);
            }
            $query = $this->db->get();
            $s = "
              <table align=\"center\" cellspacing=\"1\" cellpadding=\"2\" class=\"tbl\">
                <tbody>
                  <tr>
                    <th class='number'>No</th>
                    <th>Nama</th>
                    <th>Nama Lengkap</th>
			        <th>Grup</th>                    

                    ";
            if ($ro==false) {
                $s .= " 
                        <th colspan='2'> Aksi </th>";
            }

            $s .= "</tr>
            ";
            if ($num > 0) {
                $i = ($num * ($offset/$num)) + 1;
            } else {
                $i = 1;
            }

            
            
            foreach ($query->result() as $row) {
                
                
                if ($i % 2 == 1 ) {
                    $kelas = "event2";
                } else {
                    $kelas = "event";
                }
               
                    
                $grup = $this->getData('nama', 'grup', 'idpta_grup = '.$row->pta_grup_id.'');
                              
                $s .= "
              <tr class=\"$kelas\">
                
                <td><div align=\"right\">$i.</div></td>                
                <td><div>".$row->namauser."</div></td>
                <td><div>".$row->nama_asli."</div></td>
                <td><div align=\"center\">$grup</div></td>
                
                ";
                if ($ro==false) {
                    $s .= "
                    
                    <td><div align='center'> <a href='".base_url()."index_arsip.php/main_user/edit_user/".$row->namauser."' title=\"Ubah Data\" class=\"edit\"><img src='".base_url()."templates/images/edit.gif' border='0'></a></div></td>   
                    <td><div align='center'> <a href=\"javascript:deleteRow('".$row->namauser."')\"><img src='".base_url()."templates/images/del.gif' border='0'></a></div></td>";
                    
                }
                $s .= "
              </tr>";

                $i++;
            }
            $s .= "</table>";
            return $s;
        }

        
        function entry_user($url,$namauser='',$nama_lengkap='',$passwd='',$email='',$id_grup='') {
            
            if ($namauser =='')
                $box = "<input name='namauser' id='namauser' type='text' size='15' value='$namauser' class='required' title='Nama harus diisi' />";
            else
                $box = "<input name='namauser' id='namauser' type='hidden' size='15' value='$namauser' /> $namauser";                                    

            $ret = "<fieldset>
            <form id='tambahuser' method='post' action='".base_url()."index_arsip.php/main_user/$url'>
              <table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">      
                  <tr>
                    <td class=\"kolom\" valign=\"top\">Username </td>
                    <td>:</td>
                    <td>$box</td>
                  </tr>
                  <tr>
                    <td class=\"kolom\" valign=\"top\">Nama Lengkap </td>
                    <td>:</td>
                    <td><input name='nama_lengkap' id='nama_lengkap' type='text' size='25' value='$nama_lengkap'/></td>
                  </tr>
                  <tr>
                    <td class=\"kolom\" valign=\"top\">Password </td>
                    <td>:</td>
                    <td><input name='passwd' id='passwd' type='password' size='15'  /></td>
                  </tr>
                  <tr>
                    <td class=\"kolom\" valign=\"top\">Ulangi Password </td>
                    <td>:</td>
                    <td><input name='passwd2' id='passwd2' type='password' size='15' /></td>
                  </tr>
                  <tr>
                    <td class=\"kolom\" valign=\"top\">Email </td>
                    <td>:</td>
                    <td><input name='email' id='email' type='text' size='50' value='$email'/></td>
                  </tr>
                  <tr>
                    <td class=\"kolom\" valign=\"top\">Grup </td>
                    <td>:</td>
                    <td>".combo_jenis_admin('jenis_admin',$id_grup,'')."</td>
                  </tr>
               </table>
               <p>
                  <div >
                          <input class=\"button\" type=\"button\" value=\"<< Kembali\" name=\"btn_kembali\" id=\"btn_kembali\" />&nbsp;
                          <input class=\"button\" type=\"submit\" value=\"Simpan\" name=\"simpan\" />
                  </div>
               </p>
                </form></fieldset>";
                return $ret;
        }
        
        function simpan_user($namauser,$nama_lengkap,$passwd,$email,$id_grup,$is_update=FALSE) {
            $arr['namauser'] = $namauser;            
            $arr['nama_asli'] = $nama_lengkap;
            if ($passwd!='') {
                $arr['passwd'] = $passwd;
                $arr['hint'] = $passwd; 
            }
            $arr['pta_grup_id'] = $id_grup;          
            $arr['email'] = $email;
            
            if ($is_update==FALSE) {
                $this->db->insert('user', $arr);
            } else {
                $this->db->where("namauser", $namauser);
                $this->db->update('user', $arr);
            }
        }
        
        function list_grup($where="", $num=0, $offset=0, $ro=false) {
            $this->db->from('grup');
            if ($where!='') {
                $this->db->where($where);
            }
            
            if ($num > 0) {
                $this->db->limit($num, $offset);
            }
            $query = $this->db->get();
            $s = "
              <table align=\"center\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\" class=\"tbl\">
                <tbody>
                  <tr>
                    <th class='number' width=15>No</th>
                    <th>Nama Grup</th>                    

                    ";
            if ($ro==false) {
                $s .= " <th colspan='2'> Aksi </th>";
            }

            $s .= "</tr>
            ";
            if ($num > 0) {
                $i = ($num * ($offset/$num)) + 1;
            } else {
                $i = 1;
            }

 
            foreach ($query->result() as $row) {
                if ($i % 2 == 1 ) {
                    $kelas = "event2";
                } else {
                    $kelas = "event";
                }
  
                              
                $s .= "
              <tr class=\"$kelas\" id=\"row-".$row->idpta_grup."\" >
                <td><div align=\"right\">$i.</div></td>
                <td><div>".$row->nama."</div></td>

                
                ";
                if ($ro==false) {
                    $s .= "

                  
                    <td><div align='center'> <a href='".base_url()."index_arsip.php/main_user/edit_grup/".$row->idpta_grup."' title=\"Ubah Data\" class=\"edit\"><img src='".base_url()."templates/images/edit.gif' border='0'></a></div></td>   
                    <td><div align='center'><a href=\"javascript:deleteRow(".$row->idpta_grup.")\"><img src='".base_url()."templates/images/del.gif' border='0'></a></div></td>";
                    
                    
                }
                $s .= "
              </tr>";

                $i++;
            }
            $s .= "</table>";
            return $s;
        }
        
		function entry_grup($url='',$nama='', $id=0,$edit=''){
            $ret = "<fieldset>
            
            <form method='post' action='$url' id='grup'>
               <table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\"> 
                  
                  <tr>
                    <td class=\"kolom\" valign=\"top\">Nama  </td>
                    <td>:</td>
                    <td><input name='nama' id='nama' type='text' size='15' value='$nama'  />
                        <input name='id' id='id' type='hidden' size='15' value='$id'  /></td>
                  </tr>
                  <tr>
                    <td > </td>
                    <td> </td>
                    <td><input class=\"button\" type=\"button\" value=\"<< Kembali\" name=\"btn_kembali\" id=\"btn_kembali\" />&nbsp;
                          <input class=\"button\" type=\"submit\" value=\"Simpan\" name=\"simpan\" />
                  </tr>
                  </table>
                  
                          
                          
                </form></fieldset>";
                return $ret;
        }
        
        
        function simpan_grup($table,$nama,$id=0,$is_update=FALSE) {           
            $arr['nama'] = $nama;            
            if ($is_update==FALSE) {
                $this->db->insert($table, $arr);
            } else {
                $this->db->where("idpta_grup", $id);
                $this->db->update($table, $arr);
            }
        }
        
        
	}
?>